using System;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

using ProyectoFinal.Controles;

namespace ProyectoFinal.HandlerDB
{

    public class Parametro
    {
        public string nombre_col;
        public object val_col;
        public System.Data.SqlDbType tipo;
        public Parametro() { }

        public Parametro(string nom, object val,  System.Data.SqlDbType tipo)
        {
            nombre_col = nom;
            val_col = val;
            this.tipo = tipo;
        }
   

    }

    public class Procedimiento
    {
        //string strcon = "Data Source=PATMOS;Initial Catalog=IngieneriaSoftware;Integrated Security=True;User Id=sa; Password=";
        //string strcon = "Data Source=PATMOS;Initial Catalog=IngieneriaSoftware;User Id=sa; Password=operador;Max Pool size=200";
        string NomProc;
        SqlCommand cmd;
        //Dim param As Data.IDbDataParameter
        SqlConnection dbConnection;

        string strcon;

        public Procedimiento() { }
        public Procedimiento(string strcon) {

            this.strcon = strcon;
        
        }

        public  Procedimiento(string nom, string strcon)
            : base()
        {
            //constructor()

            dbConnection = new SqlConnection(strcon);
            dbConnection.Open();

            cmd = new SqlCommand();
            cmd.Connection = dbConnection;
            cmd.CommandText = nom;
            cmd.CommandType = CommandType.StoredProcedure;
            //cmd.Connection = New SqlClient.SqlConnection(cfg.ConnectionString)
        }

        public void Agregar(string nombre, object val, System.Data.SqlDbType tipo)
        {
            SqlParameter param = new SqlParameter();

            param.ParameterName = "@" + nombre;
            param.Direction = ParameterDirection.Input;
            param.Value = val;
            param.SqlDbType = tipo;

            cmd.Parameters.Add(param);
        }

        public DataTable EjecutarConResultados()
        {
            SqlDataAdapter da = new SqlDataAdapter();
            DataTable dt = new DataTable();

            if (dbConnection.State == ConnectionState.Closed)
            {
                dbConnection.Open();
            }

            try
            {
                
                da.SelectCommand = cmd;
                da.Fill(dt);
                return dt;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);

            }
        }

        public SqlDataReader  EjecutarOneResultados()
        {

            SqlDataReader rdr;
            if (dbConnection.State == ConnectionState.Closed)
            {
                dbConnection.Open();
            }

            try
            {

                rdr =  cmd.ExecuteReader();
                //while (rdr.Read())
                //{
                //    int a = (int)rdr.GetValue(0);
                //    string b = (string)rdr.GetValue(1);


                //}
                //rdr.op
                return rdr;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);

            }
        }

        public void EjecutarNoResultados()
        {
            if (dbConnection.State == ConnectionState.Closed)
            {
                dbConnection.Open();
            }
            try
            {
                cmd.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public SqlCommand Comando
        {
            get { return cmd; }
        }
        //Set(ByVal value As Parameter)

        //End Set

    }


}
